package com.yanqu.road.server.cmd.newmonopolymkt.res;

import com.yanqu.road.entity.systemgroup.newmonopolymarket.NewMonopolyMarketUserData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.newmonopolymkt.NewMonopolyMarketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.newmonopolymkt.NewMonopolyMarketModule;
import com.yanqu.road.server.manger.NewMonopolyMarketCommonMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.systemgroup.newmonopolymarket.NewMonopolyMarketMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@CmdApi(reqMsg = NewMonopolyMarketProto.NewMonopolyMarketResGrabSearchReqMsg.class, respMsg = NewMonopolyMarketProto.NewMonopolyMarketResGrabSearchRespMsg.class)
@Cmd(code = GameProtocol.S_NEW_MONOPOLY_MARKET_RES_GRAB_SEARCH, desc = "资源搜索")
public class NewMonopolyMarketResGrabSearchCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        NewMonopolyMarketProto.NewMonopolyMarketResGrabSearchReqMsg req = NewMonopolyMarketProto.NewMonopolyMarketResGrabSearchReqMsg.parseFrom(packet.getBytes());
        if(!NewMonopolyMarketMgr.canEnterSystem(player)){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        if(!NewMonopolyMarketCommonMgr.isSystemOpenTime(req.getResType())){
            ret = GameErrorCode.E_NEW_MONOPOLY_MARKET_SYSTEM_NO_OPEN;
            return;
        }
        NewMonopolyMarketUserData userData = player.getModule(NewMonopolyMarketModule.class).userData;
        if(userData == null){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        if(userData.getGuanYin() <= 0){
            ret = GameErrorCode.E_GOODS_NO_EXIST;
            return;
        }
        if(req.getSearchType() == 1){
            userData.getSearchPointMap().remove(req.getResGroupId());
            userData.setUpdateOption();
        }
        if(userData.getSearchPointMap().containsKey(req.getResGroupId())){
            ret = GameErrorCode.E_NEW_MONOPOLY_MARKET_HAS_SEARCH_RESULT;
            return;
        }
        NewMonopolyMarketProto.CrossNewMonopolyMarketResGrabSearchReqMsg.Builder builder = NewMonopolyMarketProto.CrossNewMonopolyMarketResGrabSearchReqMsg.newBuilder();
        builder.setGuanYin(userData.getGuanYin());
        builder.setClientReq(req);
        builder.setUserInfo(PlayerPb.parsePlayerBaseTempMsg(player.getUserInfo()));
        NewMonopolyMarketMgr.sendAppointActivityPacket(player.getUserId(), CrossProtocol.C_CROSS_NEW_MONOPOLY_MARKET_RES_GRAB_SEARCH, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            NewMonopolyMarketProto.NewMonopolyMarketResGrabSearchRespMsg.Builder builder = NewMonopolyMarketProto.NewMonopolyMarketResGrabSearchRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_NEW_MONOPOLY_MARKET_RES_GRAB_SEARCH, builder);
        }
    }

}
